package guij.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import guij.taobao.TopInterface;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.taobao.api.model.Item;
import com.taobao.api.model.SellerCat;

// 用户访问应用
public class OnTopCallBack implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		if (arg0.getParameter("top_parameters") != null
				&& arg0.getParameter("top_session") != null
				&& arg0.getParameter("top_sign") != null) {

			String top_parameters = arg0.getParameter("top_parameters")
					.toString();
			String top_session = arg0.getParameter("top_session").toString();
			String top_sign = arg0.getParameter("top_sign").toString();

			boolean bverify = topInterface.verifyTopResponse(top_parameters,
					top_session, top_sign);
			if (bverify) {
				String top_nick = topInterface.ParametersName(top_parameters);
				System.out.println("nick:"+top_nick);
				arg0.getSession().setAttribute("top_nick", top_nick);
				arg0.getSession().setAttribute("top_session", top_session);

				List<Item> items = topInterface.getOnSaleItems(top_session, 30, null);
				List<SellerCat> sellerCids = topInterface.getSellerCids(top_nick);
				Map ret = new HashMap();
				
				ret.put("items", items);
				ret.put("sellerCids",sellerCids);
				
				System.out.println("items:"+items.size());
				System.out.println("sellerCids:"+sellerCids.size());
				System.out.println("auth true");
				return new ModelAndView(getViewPage(),ret);
			}else{
				System.out.println("auth false");
				return new ModelAndView(getErrorPage());
			}
		}
		return new ModelAndView(getErrorPage());
	}

	public void setViewPage(String viewPage) {
		this.viewPage = viewPage;
	}

	public String getViewPage() {
		return viewPage;
	}

	public void setTopInterface(TopInterface topInterface) {
		this.topInterface = topInterface;
	}

	public TopInterface getTopInterface() {
		return topInterface;
	}

	public String getErrorPage() {
		return errorPage;
	}

	public void setErrorPage(String errorPage) {
		this.errorPage = errorPage;
	}

	private String viewPage;
	private String errorPage;

	private TopInterface topInterface;
}
